using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{

public class  CLSMAECOLP:ObservableObject
{

MDOgeneral mod = new MDOgeneral();
#region metodos basicos
    public List<CLSMAECOLP> listaColor()
    {
        try
        {
            string query = "select CODCOL_COL,NOMCOL_COL from MAECOLP order by NOMCOL_COL";
            DataTable val = mod.consulta(query);
            List<CLSMAECOLP> lisColor = new List<CLSMAECOLP>();
            foreach (DataRow r in val.Rows)
            {
                lisColor.Add(new CLSMAECOLP { CODCOL_COL = r["CODCOL_COL"].ToString(), NOMCOL_COL = r["NOMCOL_COL"].ToString() });
            }
            return lisColor;
        }
        catch (Exception e) { throw e; }
    }

    public List<CLSMAECOLP> lisColorMaterial(string strMaterial)
    {
        try
        {
            string query = "select b.CODCOL_COL, b.NOMCOL_COL from maemtp a inner join maecolp b on a.codcol_mtp=b.codcol_col where a.codmat_mtp='" + strMaterial + "' order by b.NOMCOL_COL";
            DataTable val = mod.consulta(query);
            List<CLSMAECOLP> lisColor = new List<CLSMAECOLP>();
            lisColor.Add(new CLSMAECOLP { CODCOL_COL = "ZZZZ", NOMCOL_COL = "Todos los Colores" });
            foreach (DataRow r in val.Rows)
            {
                lisColor.Add(new CLSMAECOLP { CODCOL_COL = r["CODCOL_COL"].ToString(), NOMCOL_COL = r["NOMCOL_COL"].ToString() });
            }
            return lisColor;
        }
        catch (Exception e) { throw e; }
    }

    public void consultar()
    {
        try
        {
            string query = "select CODCOL_COL,NOMCOL_COL from MAECOLP where (CODCOL_COL='" + CODCOL_COL + "')";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {
                this.CODCOL_COL = val.Rows[0]["CODCOL_COL"].ToString();
                this.NOMCOL_COL = val.Rows[0]["NOMCOL_COL"].ToString();
                EXISTE = true;
                CAMBIO = false;
            }
            else
            {
                EXISTE = false;
            }
        }
        catch (Exception e) { throw e; }
    }
    public int eliminar()
    {
        try
        {
            if (!exiMov())
            {
            string query = "delete from MAECOLP where (CODCOL_COL='" + CODCOL_COL + "')";
            return mod.comando(query);
            }
            return 0;
        }
        catch (Exception e) { throw e; }
    }
    public int actualizar()
    {
        try
        {
            string query = "update MAECOLP set NOMCOL_COL='" + NOMCOL_COL + "' where (CODCOL_COL='" + CODCOL_COL + "')";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public int insertar()
    {
        try
        {
                string query = "insert into MAECOLP (CODCOL_COL,NOMCOL_COL) values ('" + CODCOL_COL + "','" + NOMCOL_COL + "')";
                return mod.comando(query);
      
        }
        catch (Exception e) { throw e; }
    }
    public Boolean existe()
    {
        try
        {
            string query = "select count(*) from MAECOLP where (CODCOL_COL='" + CODCOL_COL + "')";
            DataTable val = mod.consulta(query);
            return val.Rows[0][0].ToString().Equals("0") ? false : true;
        }
        catch (Exception ex) { throw (ex); }
    }
    public Boolean exiMov()
    {
        try
        {
            //Maestro materia prima
            string query = "select count(*) from MAEMTP where CODCOL_MTP ='" + CODCOL_COL + "'";
            if (!mod.consulta(query).Rows[0][0].ToString().Equals("0"))
            {
                return true;
            }
            return false;
        }
        catch (Exception ex) { throw (ex); }
    }
   
#endregion
    
#region Otros
   


public DataTable buscar() {
  try{
  string query="select CODCOL_COL as Codigo_color,NOMCOL_COL as Nombre_color from  MAECOLP where ";

  if(NOMCOL_COL!=""){
      query=query+"UPPER(NOMCOL_COL) like '%"+NOMCOL_COL.ToUpper()+"%' and ";
  }
  query = query.Remove(query.Length - 4).Trim() + " Order by NOMCOL_COL asc";
      return mod.consulta(query);
  }catch (Exception e) { throw e; }
}

public DataTable buscaradd()
{
    try { 
    string query = "select CODCOL_COL as Codigo_color,NOMCOL_COL as Nombre_color from  MAECOLP where CODCOL_COL='"+CODCOL_COL+"'";
    return mod.consulta(query);
    }catch (Exception e) { throw e; }
}

#endregion

#region  Propiedades tabla MAECOLP
          
    private string _CODCOL_COL;
    public string CODCOL_COL{
        get { return _CODCOL_COL;}
        set { _CODCOL_COL=value;
        RaisePropertyChangedEvent("CODCOL_COL");
        }
    }
    private string _NOMCOL_COL;
    public string NOMCOL_COL{
        get { return _NOMCOL_COL;}
        set {
            if (_NOMCOL_COL != value) { CAMBIO = true; }
            _NOMCOL_COL=value;
            RaisePropertyChangedEvent("NOMCOL_COL");
        }
    }
    private Boolean _CAMBIO = false;
    public Boolean CAMBIO
    {
        get { return _CAMBIO; }
        set
        {
            _CAMBIO = value;
            RaisePropertyChangedEvent("CAMBIO");
        }
    }
    private Boolean _EXISTE = false;
    public Boolean EXISTE
    {
        get { return _EXISTE; }
        set
        {
            _EXISTE = value;
            RaisePropertyChangedEvent("EXISTE");
        }
    }

#endregion

   }
}
